package com.woniuxy.controller.commonController;

import com.woniuxy.demo.Result;
import com.woniuxy.util.OSSClientUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@RestController
@RequestMapping("/file")
@Api(tags = "公用接口文件接口，用于上传等处理")
public class FileController {
    @Autowired
    private OSSClientUtil ossClientUtil;

    /**
     * 上传文件去阿里云
     *
     * @param file
     * @return
     */
    @ApiOperation("上传文件去阿里云")
    @PostMapping("upload")
    public Result upload(MultipartFile file) {
        String name = file.getOriginalFilename();
        name = name.substring(name.lastIndexOf("."));
        String fileName = System.currentTimeMillis() + name;
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String url = ossClientUtil.uploadFile(fileName, inputStream);
        return Result.newInstance().setCode(200).setMsg("上传成功").setData(url);
    }
}
